package com.xingdata.jjxc.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.RequestCode;
import com.xingdata.jjxc.adapter.CarInfoListViewAdapter;
import com.xingdata.jjxc.base.Base1Activity;
import com.xingdata.jjxc.enty.CarInfoEntity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.http.HttpResponseListener;
import com.xingdata.jjxc.utils.SystemInfo;

public class CarInfoListViewActivity extends Base1Activity implements OnClickListener, OnItemClickListener {
	private TextView title;
	private ListView carlist_lv;
	private CarInfoListViewAdapter carInfoListViewAdapter;
	private List<CarInfoEntity> carInfoEntityList = new ArrayList<CarInfoEntity>();
	private CarInfoEntity carInfoEntity;
	/** 统一定义bundle */
	private Bundle bundle;
	private int add_flag;// 0新增 1修改
	private int position;
	
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				carInfoListViewAdapter.notifyDataSetChanged();
				break;
			}

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		dopost_getcarlist();

	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.title_back:
			finish();
			break;

		case R.id.addButton:
			Intent intent = new Intent(CarInfoListViewActivity.this, CarInforAddActivity.class);
			Bundle bundle = new Bundle();
			bundle.putInt("add_flag", 0);
			intent.putExtras(bundle);
			startActivityForResult(intent, RequestCode.REQUESTCODE_CAR_INFO);
			break;

		default:
			break;
		}
	}

	@Override
	protected int getContentViewId() {
		// TODO Auto-generated method stub
		return R.layout.carinfo_listview;
	}

	@Override
	protected String getTitleText() {
		// TODO Auto-generated method stub
		return "爱车列表";
	}

	@Override
	protected void initViews() {

		((Button) findViewById(R.id.addButton)).setOnClickListener(this);
		
		((ImageView) findViewById(R.id.title_back)).setOnClickListener(this);
		title = (TextView) findViewById(R.id.title);
		title.setText("爱车列表");
		
		carlist_lv = (ListView) findViewById(R.id.carlist_lv);
		carInfoListViewAdapter = new CarInfoListViewAdapter(CarInfoListViewActivity.this, carInfoEntityList);
		carlist_lv.setAdapter(carInfoListViewAdapter);
		carlist_lv.setOnItemClickListener(this);
	}

	/** 爱车列表信息 */
	private void dopost_getcarlist() {
		paramsMap = new HashMap<String, String>();
		
		paramsMap.put("userid", SystemInfo.getUserInfo().getUserid());
		paramsMap.put("access_token", SystemInfo.getUserInfo().getToken());

		mHttpUtil.Post(App.ZZD_REQUEST_GETCARLIST, paramsMap, new HttpResponseListener() {

			@Override
			public void onSuccess(RespEntity respEntity) {
				carInfoEntityList.addAll(JSON.parseArray(respEntity.getResult(), CarInfoEntity.class));
				carInfoListViewAdapter.notifyDataSetChanged();
			}

			@Override
			public void onLoading(int vurrent) {

			}

			@Override
			public void onFailure(String result) {

			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
		Intent intent = new Intent(this, CarInforAddActivity.class);
		add_flag = 1;
		bundle = new Bundle();
		bundle.putSerializable("entity", carInfoEntityList.get(position));
		bundle.putInt("add_flag", add_flag);// 0添加 1 修改
		intent.putExtras(bundle);
		startActivityForResult(intent, RequestCode.REQUESTCODE_UPDATA_CAR_INFO);
		// carInfoEntityList.remove(position);
		this.position = position;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		// carInfoEntityList.addAll(0,(Collection<? extends CarInfoEntity>)
		// addCarInfoEntity);
		// carInfoListViewAdapter.notifyDataSetChanged();
		// Message msg=new Message();
		// msg.what=1;
		// handler.sendMessage(msg);
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode != RESULT_OK) {
			return;
		}
		switch (requestCode) {
		case RequestCode.REQUESTCODE_CAR_INFO:

			carInfoEntityList.add(0, (CarInfoEntity) data.getExtras().getSerializable("addcarinfoentity"));

			Message msg = new Message();
			msg.what = 1;
			handler.sendMessage(msg);
			break;
		case RequestCode.REQUESTCODE_UPDATA_CAR_INFO:

			carInfoEntityList.set(position, (CarInfoEntity) data.getExtras().getSerializable("addcarinfoentity"));

			Message msg1 = new Message();
			msg1.what = 1;
			handler.sendMessage(msg1);
		default:
			break;
		}

	}
}
